home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / raytrace / rayshade / etc / off / off2ray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-05  |  4.0 KB  |  185 lines

  1. /*
  2.  * off2ray.c
  3.  *
  4.  * Convert OFF format objects to rayshade format.
  5.  *
  6.  * Warning!  Hastily thrown-together code follows.
  7.  *
  8.  * usage:
  9.  * off2ray objname > objname.ray    (default vertex order)
  10.  * or
  11.  * off2ray objnamne yes > objname.ray    (reverse vertex order)
  12.  *
  13.  * Reads 'objname.geom', as well as 'objname.vnorm' and 'objname.pcol'
  14.  * if they exist, and writes a rayshade-compatible object description
  15.  * to the standard output.
  16.  *
  17.  * Note that you'll want to hand-tweak the surface definitions.
  18.  *
  19.  * Craig Kolb 12/89
  20.  *
  21.  * Indexed polygon color changes by Tom Friedel, 1/90
  22.  */
  23. #include <stdio.h>
  24.  
  25. #define TRUE    1
  26. #define FALSE    0
  27. #define usage()        fprintf(stderr,"usage: off2ray basename\n")
  28.  
  29. typedef struct {
  30.     float x, y, z;
  31. } Vertex;
  32.  
  33. FILE *fgeom, *fnorm, *fpcol;
  34. Vertex *vertices, *normals, *surfaces;
  35. int Nvert, Nnorm, Npoly, Nsurface, *surfindex, Ncols;
  36. int NoNormals, NoPcol, ReverseOrder;
  37.  
  38. main(argc, argv)
  39. int argc;
  40. char **argv;
  41. {
  42.     char buf[BUFSIZ];
  43.     int porder, norder, pnum, nnum, junk, i, j;
  44.     float red, green, blue;
  45.  
  46.     if (argc != 2 && argc != 3) {
  47.         usage();
  48.         exit(2);
  49.     }
  50.  
  51.     if (argc == 3)
  52.         ReverseOrder = TRUE;
  53.  
  54.     sprintf(buf,"%s.geom",argv[1]);
  55. #ifdef __WATCOMC__
  56.     fgeom = fopen(buf, "rb");
  57. #else
  58.     fgeom = fopen(buf, "r");
  59. #endif
  60.     if (fgeom == (FILE *)NULL) {
  61.         fprintf(stderr,"Cannot open geometry file %s\n",buf);
  62.         exit(3);
  63.     }
  64.     /*
  65.      * Read geometry file header and read in vertices.
  66.      */
  67.     fscanf(fgeom,"%d %d %d",&Nvert, &Npoly, &junk);
  68.     vertices = (Vertex *)malloc(Nvert * sizeof(Vertex));
  69.     for (i = 0; i < Nvert; i++)
  70.         fscanf(fgeom,"%f %f %f",&vertices[i].x, &vertices[i].y,
  71.                     &vertices[i].z);
  72.  
  73.     sprintf(buf,"%s.vnorm",argv[1]);
  74. #ifdef __WATCOMC__
  75.     fnorm = fopen(buf, "rb");
  76. #else
  77.     fnorm = fopen(buf, "r");
  78. #endif
  79.     if (fnorm == (FILE *)NULL)
  80.         NoNormals = TRUE;
  81.     else {
  82.         /*
  83.          * Read normal file header and normals.
  84.          */
  85.         fscanf(fnorm,"%d %d %d",&Nnorm, &Npoly, &junk);
  86.         normals = (Vertex *)malloc(Nnorm * sizeof(Vertex));
  87.         for (i = 0; i < Nnorm; i++)
  88.             fscanf(fnorm,"%f %f %f",&normals[i].x,
  89.                         &normals[i].y,
  90.                         &normals[i].z);
  91.     }
  92.  
  93.     sprintf(buf,"%s.ipcol",argv[1]);
  94. #ifdef __WATCOMC__
  95.     fpcol = fopen(buf, "rb");
  96. #else
  97.     fpcol = fopen(buf, "r");
  98. #endif
  99.     if (fpcol == (FILE *)NULL)
  100.         NoPcol = TRUE;
  101.     else {
  102.         fscanf(fpcol,"%d %d",&Ncols, &Npoly);
  103.         surfaces = (Vertex *)malloc(Npoly * sizeof(Vertex));
  104.         surfindex = (int *)malloc(Npoly * sizeof(int));
  105.         for (i = 0; i < Ncols; i++) {
  106.             fscanf(fpcol,"%f %f %f", &red, &green, &blue);
  107.             find_surface(red, green, blue);
  108.         }
  109.         for (i = 0; i < Npoly; i++) {
  110.             fscanf(fpcol,"%d", & surfindex[ i ] ) ;
  111.             surfindex[ i ] -- ;
  112.         }
  113.         print_surfaces();
  114.     }
  115.  
  116.     for (i = 0; i < Npoly; i++) {
  117.         fscanf(fgeom,"%d",&porder);
  118.         if (!NoNormals) {
  119.             fscanf(fnorm, "%d",&porder);
  120.             if (porder != 3) {
  121.                 fprintf(stderr,"%d-sided poly!\n",porder);
  122.                 exit(10);
  123.             }
  124.         }
  125.         if (porder == 3)
  126.             printf("triangle ");
  127.         else
  128.             printf("poly ");
  129.         if (NoPcol)
  130.             printf("poly_surf ");
  131.         else
  132.             printf("poly_surf%d ",surfindex[i]);
  133.         print_poly(porder);
  134.     }
  135. }
  136.  
  137. print_poly(order)
  138. int order;
  139. {
  140.     int pnum, nnum;
  141.  
  142.     if (order == 0)
  143.         return;
  144.  
  145.     fscanf(fgeom,"%d",&pnum);
  146.     pnum--;
  147.     if (!NoNormals) {
  148.         fscanf(fnorm,"%d",&nnum);
  149.         nnum--;
  150.     }
  151.     if (ReverseOrder)
  152.         print_poly(order -1);
  153.     printf("%f %f %f",vertices[pnum].x, vertices[pnum].y,
  154.                     vertices[pnum].z);
  155.     if (NoNormals)
  156.         printf("\n");
  157.     else if (!ReverseOrder)
  158.         printf(" %f %f %f\n",-normals[nnum].x, -normals[nnum].y,
  159.                     -normals[nnum].z);
  160.     else
  161.         printf(" %f %f %f\n",normals[nnum].x, normals[nnum].y,
  162.                     normals[nnum].z);
  163.     if (!ReverseOrder)
  164.         print_poly(order -1);
  165.  
  166. find_surface(r, g, b)
  167. float r, g, b;
  168. {
  169.     surfaces[Nsurface].x = r;
  170.     surfaces[Nsurface].y = g;
  171.     surfaces[Nsurface].z = b;
  172.     Nsurface++;
  173.  
  174. }
  175.  
  176. print_surfaces()
  177. {
  178.     int i;
  179.  
  180.     for (i = 0; i < Nsurface; i++)
  181.         printf("surface poly_surf%d 0 0 0  %f %f %f  0 0 0 0 0 0 0\n",
  182.             i, surfaces[i].x, surfaces[i].y, surfaces[i].z);
  183. }
  184.